﻿using System;
using System.Collections.Generic;
using Model;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;

namespace WebUI.LabManage
{
    public partial class LabEquipmentEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SelectPanel();
                BindForm();
                Cancel.OnClientClick = ExtAspNet.ActiveWindow.GetConfirmHideReference();
                Cancel2.OnClientClick = ExtAspNet.ActiveWindow.GetConfirmHideReference();
                Close.OnClientClick = ExtAspNet.ActiveWindow.GetConfirmHideReference();
            }
            if (Request[Common.LinkService.RefreshTag] == "closeWindow")
            {
                ExtAspNet.PageContext.RegisterStartupScript(ExtAspNet.ActiveWindow.GetHidePostBackReference("refreshParent"));
            }
        }

        private void BindForm()
        {
            string action = Request.QueryString["action"];
            int id = 0;
            if (Request.QueryString["id"] != null)
            {
                id = int.Parse(Request.QueryString["id"]);
            }
            if (action == "new")
            {
                ExtAspNet.ListItem item = new ExtAspNet.ListItem("请选择", "-1");
                dl_type.DataSource = BLL.LabEquipment.GetParamList(6);
                dl_type.DataTextField = "value";
                dl_type.DataValueField = "id";
                dl_type.DataBind();
                dl_type.Items.Insert(0,item);
                dl_lab.DataSource = BLL.LabEquipment.GetLabList();
                dl_lab.DataTextField = "LabName";
                dl_lab.DataValueField = "LabID";
                dl_lab.DataBind();
                dl_lab.Items.Insert(0, item);
                dl_usetype.DataSource = BLL.LabEquipment.GetParamList(7);
                dl_usetype.DataTextField = "value";
                dl_usetype.DataValueField = "id";
                dl_usetype.DataBind();
                dl_usetype.Items.Insert(0, item);
            }
            if (action == "edit")
            {
                dl_edit_type.DataSource = BLL.LabEquipment.GetParamList(6);
                dl_edit_type.DataTextField = "value";
                dl_edit_type.DataValueField = "id";
                dl_edit_type.DataBind();
                dl_edit_lab.DataSource = BLL.LabEquipment.GetLabList();
                dl_edit_lab.DataTextField = "LabName";
                dl_edit_lab.DataValueField = "LabID";
                dl_edit_lab.DataBind();
                dl_edit_usetype.DataSource = BLL.LabEquipment.GetParamList(7);
                dl_edit_usetype.DataTextField = "value";
                dl_edit_usetype.DataValueField = "id";
                dl_edit_usetype.DataBind();
                Model.LabEquipment m = BLL.LabEquipment.SelectLabEquipmentByID(id);
                tb_edit_name.Text = m.Name;
                tb_edit_pNO.Text = m.propertyNO;
                tb_edit_price.Text = m.price.ToString();
                tb_edit_saller.Text = m.seller;
                dl_edit_lab.SelectedValue = m.LabID.ToString();
                dl_edit_type.SelectedValue = m.type.ToString();
                dl_edit_usetype.SelectedValue = m.usetype.ToString();
                dp_edit_buydate.SelectedDate = m.buydate;
                ta_edit_functions.Text = m.functions;
                tb_edit_brand.Text = m.brand;
                tb_edit_model.Text = m.model;
                tb_edit_count.Text = m.count.ToString();
                tb_edit_manager.Text = m.manager;
            }
            if (action == "view")
            {
                Model.LabEquipment m = BLL.LabEquipment.SelectLabEquipmentByID(id);
                lb_name.Text = m.Name;
                lb_pNO.Text = m.propertyNO;
                lb_type.Text = m.typeParam.value;
                lb_lab.Text = m.Lab.LabName;
                lb_status.Text = m.statusParam.value;
                lb_saller.Text = m.seller;
                lb_price.Text = string.Format("{0:C}",m.price);
                if (m.buydate.ToString() != "")
                {
                    lb_buydate.Text = m.buydate.Value.ToLongDateString();
                }
                lb_functions.Text = m.functions;
                lb_usetype.Text = m.usetypeParam.value;
                lb_count.Text = m.count.ToString();
                lb_brand.Text = m.brand;
                lb_model.Text = m.model;
                lb_manager.Text = m.manager;
                //GRID绑定
                RentGridBind(id);
                FixGridBind(id);
                //按钮事件绑定
                bnAbandon.OnClientClick = eq_window.GetShowReference(string.Format("LabEquipmentEdit.aspx?action=aban&id={0}",id), "设备报废登记");
                bnRent.OnClientClick = eq_window.GetShowReference(string.Format("LabEquipmentEdit.aspx?action=rent&id={0}", id), "设备借出登记");
                bnReturn.OnClientClick = eq_window.GetShowReference(string.Format("LabEquipmentEdit.aspx?action=return&id={0}", id), "设备归还入库");
                bnFix.OnClientClick = eq_window.GetShowReference(string.Format("LabEquipmentEdit.aspx?action=repair&id={0}", id), "设备报修登记");
                bnFixOK.OnClientClick = eq_window.GetShowReference(string.Format("LabEquipmentEdit.aspx?action=fixok&id={0}", id), "设备维修反馈");
                switch (m.status)
                {
                    case 29:
                        bnRent.Hidden = false;
                        bnFix.Hidden = false;
                        bnWaiting.Hidden = false;
                        break;
                    case 46:
                        bnRent.Hidden = false;
                        bnFix.Hidden = false;
                        bnUsing.Hidden = false;
                        break;
                    case 30:
                        bnFixOK.Hidden = false;
                        bnAbandon.Hidden = true;
                        break;
                    case 31:
                        bnAbandon.Hidden = true;
                        break;
                    case 35:
                        bnReturn.Hidden = false;
                        bnAbandon.Hidden = true;
                        break;
                }
            }
            if (action == "aban")
            {
                Model.LabEquipment m = BLL.LabEquipment.SelectLabEquipmentByID(id);
                aban_pNO.Text = m.propertyNO;
                aban_name.Text = m.Name;
                aban_type.Text = m.typeParam.value;
            }
            if (action == "rent")
            {
                Model.LabEquipment m = BLL.LabEquipment.SelectLabEquipmentByID(id);
                rent_lb_pNO.Text = m.propertyNO;
                rent_lb_name.Text = m.Name;
            }
            if (action == "return")
            {
                Model.EquipmentRent m = BLL.EquipmentRecord.Rent.SelectRentNoReturnByEID(id);
                return_hf_rentID.Text = m.id.ToString();
                return_hf_EID.Text = m.EID.ToString();
                return_lb_name.Text = m.Equipment.Name;
                return_lb_pNO.Text = m.Equipment.propertyNO;
                return_lb_renter.Text = m.renter;
                return_lb_rentDate.Text = m.rentDate.ToLongDateString();
                return_lb_timeDay.Text = string.Format("{0} 天",m.timeDay);
                return_lb_allower.Text = m.allower;
            }
            if (action == "repair")
            {
                Model.LabEquipment m = BLL.LabEquipment.SelectLabEquipmentByID(id);
                repair_lb_pNO.Text = m.propertyNO;
                repair_lb_name.Text = m.Name;
                repair_dl_RepairType.DataSource = BLL.LabEquipment.GetParamList(8);
                repair_dl_RepairType.DataTextField = "value";
                repair_dl_RepairType.DataValueField = "id";
                repair_dl_RepairType.DataBind();
            }
            if (action == "fixok")
            {
                Model.EquipmentRepair m = BLL.EquipmentRecord.Repair.SelectRepariNoReturnByEID(id);
                fixok_lb_pNO.Text = m.Equipment.propertyNO;
                fixok_lb_name.Text = m.Equipment.Name;
                fixok_lb_cause.Text = m.RepairCause;
                fixok_lb_repairType.Text = m.RepairTypeParam.value;
                fixok_lb_reportDate.Text = m.reportDate.ToLongDateString();
                fixok_tb_timeDay.Text = m.timeDay.ToString();
                fixok_hf_id.Text = m.ID.ToString();
            }
        }

        private void FixGridBind(int EID)
        {
            string strWhere = string.Format("EID = {0}", EID);
            FixGrid.DataSource = BLL.EquipmentRecord.Repair.GetList(strWhere);
            FixGrid.DataBind();
        }

        private void RentGridBind(int EID)
        {
            string strWhere = string.Format("EID = {0}",EID);
            RentGrid.DataSource = BLL.EquipmentRecord.Rent.GetList(strWhere);
            RentGrid.DataBind();
        }

        private void SelectPanel()
        {
            string action = Request.QueryString["action"];
            if(action == "new")
            {
                PageManager1.AutoSizePanelID = "newPanel";
                newPanel.Hidden = false;
            }
            else if(action == "edit")
            {
                PageManager1.AutoSizePanelID = "editPanel";
                editPanel.Hidden = false;
            }
            else if(action == "view")
            {
                PageManager1.AutoSizePanelID = "viewPanel";
                viewPanel.Hidden = false;
            }
            else if (action == "aban")
            {
                PageManager1.AutoSizePanelID = "abanPanel";
                abanPanel.Hidden = false;
            }
            else if (action == "rent")
            {
                PageManager1.AutoSizePanelID = "rentPanel";
                rentPanel.Hidden = false;
            }
            else if (action == "return")
            {
                PageManager1.AutoSizePanelID = "returnPanel";
                returnPanel.Hidden = false;
            }
            else if (action == "repair")
            {
                PageManager1.AutoSizePanelID = "repairPanel";
                repairPanel.Hidden = false;
            }
            else if (action == "fixok")
            {
                PageManager1.AutoSizePanelID = "fixokPanel";
                fixokPanel.Hidden = false;
            }
        }

        protected void bnSave_Click(object sender, EventArgs e)
        {
            Model.LabEquipment m = new Model.LabEquipment();
            m.Name = tb_name.Text;
            m.propertyNO = tb_pNO.Text;
            m.seller = tb_saller.Text;
            m.price = decimal.Parse(tb_price.Text);
            m.LabID = int.Parse(dl_lab.SelectedValue);
            m.type = int.Parse(dl_type.SelectedValue);
            m.status = 29;
            m.buydate = dp_buydate.SelectedDate;
            m.functions = ta_functions.Text;
            m.usetype = int.Parse(dl_usetype.SelectedValue);
            m.count = int.Parse(tb_count.Text);
            m.brand = tb_brand.Text;
            m.model = tb_model.Text;
            m.manager = tb_manager.Text;
            if(BLL.LabEquipment.Add(m) > 0)
            {
                ExtAspNet.PageContext.RegisterStartupScript(Alert.GetShowInTopReference("新增设备成功！", "操作提示！", MessageBoxIcon.Information) + ExtAspNet.ActiveWindow.GetHidePostBackReference("refreshParent"));
            }
            else
            {
                ExtAspNet.Alert.Show("保存失败！", "失败", ExtAspNet.MessageBoxIcon.Error);
            }
        }
        protected void bnEdit_Click(object sender, EventArgs e)
        {
            Model.LabEquipment m = new Model.LabEquipment();
            m.ID = int.Parse(Request.QueryString["id"].ToString());
            m.Name = tb_edit_name.Text;
            m.propertyNO = tb_edit_pNO.Text;
            m.seller = tb_edit_saller.Text;
            m.price = decimal.Parse(tb_edit_price.Text);
            m.type = int.Parse(dl_edit_type.SelectedValue);
            m.LabID = int.Parse(dl_edit_lab.SelectedValue);
            m.status = 29;
            m.buydate = dp_edit_buydate.SelectedDate;
            m.functions = ta_edit_functions.Text;
            m.usetype = int.Parse(dl_edit_usetype.SelectedValue);
            m.manager = tb_edit_manager.Text;
            m.brand = tb_edit_brand.Text;
            m.model = tb_edit_model.Text;
            m.count = int.Parse(tb_edit_count.Text);
            if(BLL.LabEquipment.Update(m))
            {
                ExtAspNet.PageContext.RegisterStartupScript(Alert.GetShowInTopReference("编辑设备成功！", "操作提示！", MessageBoxIcon.Information) + ExtAspNet.ActiveWindow.GetHidePostBackReference("refreshParent"));
            }
            else
            {
                ExtAspNet.Alert.Show("编辑失败！", "失败", ExtAspNet.MessageBoxIcon.Error);
            }
        }
        protected void bnWaiting_Click(object sender, EventArgs e)
        {
            int id = int.Parse(Request.QueryString["id"].ToString());
            if (BLL.LabEquipment.WaitLabEquipmentByID(id))
            {
                ExtAspNet.PageContext.RegisterStartupScript(Alert.GetShowInTopReference("设备待处理成功！", "操作提示！", MessageBoxIcon.Information) + ExtAspNet.ActiveWindow.GetHidePostBackReference("refreshParent"));
            }
            else
            {
                ExtAspNet.Alert.Show("操作失败！", "失败", ExtAspNet.MessageBoxIcon.Error);
            }
        }
        protected void bnUsing_Click(object sender, EventArgs e)
        {
            int id = int.Parse(Request.QueryString["id"].ToString());
            if(BLL.LabEquipment.UsingLabEquipmentByID(id))
            {
                ExtAspNet.PageContext.RegisterStartupScript(Alert.GetShowInTopReference("设备改为在用完成！", "操作提示！", MessageBoxIcon.Information) + ExtAspNet.ActiveWindow.GetHidePostBackReference("refreshParent"));
            }
            else
            {
                ExtAspNet.Alert.Show("操作失败！", "失败", ExtAspNet.MessageBoxIcon.Error);
            }
        }
        protected void bnAban_Click(object sender, EventArgs e)
        {
            Model.EquipmentAbandon m = new Model.EquipmentAbandon();
            m.EID = int.Parse(Request.QueryString["id"].ToString());
            m.cause = aban_tb_cause.Text;
            m.checker = aban_tb_checker.Text;
            m.date = aban_dp_date.SelectedDate.Value;
            if(BLL.LabEquipment.EquipmentAbandon(m))
            {
                ExtAspNet.PageContext.RegisterStartupScript(Alert.GetShowInTopReference("设备报废完成！", "操作提示！", MessageBoxIcon.Information) + ExtAspNet.ActiveWindow.GetHidePostBackReference("closeWindow"));
            }
            else
            {
                ExtAspNet.Alert.Show("操作失败！", "失败", ExtAspNet.MessageBoxIcon.Error);
            }
        }
        protected void bnRent_Click(object sender, EventArgs e)
        {
            Model.EquipmentRent m = new EquipmentRent();
            m.EID = int.Parse(Request.QueryString["id"].ToString());
            m.renter = rent_tb_renter.Text;
            m.renterTel = rent_tb_renterTel.Text;
            m.used = rent_tb_used.Text;
            m.timeDay = int.Parse(rent_tb_timeDay.Text);
            m.rentDate = rent_dp_rentDate.SelectedDate.Value;
            m.allower = rent_tb_allower.Text;
            if (BLL.LabEquipment.EquipmentRent(m))
            {
                ExtAspNet.PageContext.RegisterStartupScript(Alert.GetShowInTopReference("设备成功借出！", "操作提示！", MessageBoxIcon.Information) + ExtAspNet.ActiveWindow.GetHidePostBackReference("closeWindow"));
            }
            else
            {
                ExtAspNet.Alert.Show("操作失败！", "失败", ExtAspNet.MessageBoxIcon.Error);
            }
        }
        protected void bnReturn_Click(object sender, EventArgs e)
        {
            Model.EquipmentRent m = new EquipmentRent();
            m.id = int.Parse(return_hf_rentID.Text);
            m.EID = int.Parse(return_hf_EID.Text);
            m.returnDate = return_dp_returnDate.SelectedDate;
            m.checker = return_tb_checker.Text;
            m.isReturn = true;
            string status = return_rbl_status.SelectedValue;
            if (BLL.LabEquipment.EquipmentReturn(m, status))
            {
                ExtAspNet.PageContext.RegisterStartupScript(Alert.GetShowInTopReference("设备归还入库成功！", "操作提示！", MessageBoxIcon.Information) + ExtAspNet.ActiveWindow.GetHidePostBackReference("closeWindow"));
            }
            else
            {
                ExtAspNet.Alert.Show("操作失败！", "失败", ExtAspNet.MessageBoxIcon.Error);
            }
        }
        protected void bnRepair_Click(object sender, EventArgs e)
        {
            Model.EquipmentRepair m = new EquipmentRepair();
            m.EID = int.Parse(Request.QueryString["id"].ToString());
            m.allower = repair_tb_allower.Text;
            m.reportDate = repair_dp_reportDate.SelectedDate.Value;
            m.RepairCause = repair_tb_cause.Text;
            m.RepairType = int.Parse(repair_dl_RepairType.SelectedValue);
            m.budget = decimal.Parse(repair_tb_budget.Text);
            if (repair_tb_timeDay.Text != "")
            {
                m.timeDay = int.Parse(repair_tb_timeDay.Text);
            }
            if (BLL.LabEquipment.EquipmentRepair(m))
            {
                ExtAspNet.PageContext.RegisterStartupScript(Alert.GetShowInTopReference("设备报修登记完成！", "操作提示！", MessageBoxIcon.Information) + ExtAspNet.ActiveWindow.GetHidePostBackReference("closeWindow"));
            }
            else
            {
                ExtAspNet.Alert.Show("操作失败！", "失败", ExtAspNet.MessageBoxIcon.Error);
            }
        }
        protected void bnFixok_Click(object sender, EventArgs e)
        {
            Model.EquipmentRepair m = new EquipmentRepair();
            m.ID = int.Parse(fixok_hf_id.Text);
            m.EID = int.Parse(Request.QueryString["id"].ToString());
            m.timeDay = int.Parse(fixok_tb_timeDay.Text);
            m.returnDate = fixok_dp_returnDate.SelectedDate;
            m.result = fixok_tb_result.Text;
            m.repairer = fixok_tb_repairer.Text;
            m.cost = int.Parse(fixok_tb_cost.Text);
            m.checker = fixok_tb_checker.Text;
            string status = fixok_rbl_status.SelectedValue;
            if (BLL.LabEquipment.EquipmentFixok(m, status))
            {
                ExtAspNet.PageContext.RegisterStartupScript(Alert.GetShowInTopReference("维修反馈入库完成！", "操作提示！", MessageBoxIcon.Information) + ExtAspNet.ActiveWindow.GetHidePostBackReference("closeWindow"));
            }
            else
            {
                ExtAspNet.Alert.Show("操作失败！", "失败", ExtAspNet.MessageBoxIcon.Error);
            }
        }
    }
}